package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Memberf;
import com.itheima.service.PhysicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/physical")
public class PhysicalController {
    @Reference
    private PhysicalService physicalService;
@Autowired
private JedisPool jedisPool;
    @RequestMapping("/findRecord")
    public Result findRecord(Integer id){
        try {

            List<Map> record = physicalService.findReportById(id);
            return new Result(true,"成功",record);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }

    @RequestMapping("/findMember")
    public Result findMember(HttpServletRequest request){
        try {
            Cookie[] cookies = request.getCookies();
            String value=null;
            for (Cookie cookie : cookies) {
                value = cookie.getValue();
            }
            String s = jedisPool.getResource().get(value);
            ObjectMapper mapper=new ObjectMapper();

            Memberf memberf = mapper.readValue(s, Memberf.class);
            Integer id =memberf.getId();
            return new Result(true,"成功",id);
        } catch (Exception e) {
            //e.printStackTrace();
            return new Result(false,"请先点击右上方进行登录");
        }

    }

}
